{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true,
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "# Created by william from lexueoude.com. 更多正版技术视频讲解，公众号：乐学FinTech\n",
    "\n",
    "Stochastic Gradient Descent:\n",
    "\n",
    "也就是说，我们可以通过不断的迭代（iteration）,去将coefficients(b)去进行优化：\n",
    "\n",
    "$$ b = b - learning \\: rate*error *x$$\n",
    "\n",
    "\n",
    "我们接下来建立一个$$y = 0.4+ 0.8*x $$"
   ]
  },
  {
   "cell_type": "code",
   "source": [
    "#make prediction\n",
    "\n",
    "def make_prediction(input_row,coefficients):\n",
    "    out_put_y_hat = coefficients[0]\n",
    "    for i in range(len(input_row)-1):\n",
    "        out_put_y_hat += coefficients[i+1] * input_row[i]\n",
    "    return  out_put_y_hat\n",
    "\n",
    "test_dataset = [[1,1],[2,3],[4,3],[3,2],[5,5]]\n",
    "test_coefficients = [0.4,0.8]\n",
    "\n",
    "for row in test_dataset:\n",
    "    y_hat = make_prediction(row,test_coefficients)\n",
    "    print(\"True Y value = %.3f, Our_Prediction = %.3f\"%(row[-1],y_hat))\n",
    "\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n",
     "is_executing": false
    }
   },
   "execution_count": 2,
   "outputs": [
    {
     "name": "stdout",
     "text": [
      "True Y value = 1.000, Our_Prediction = 1.200\n",
      "True Y value = 3.000, Our_Prediction = 2.000\n",
      "True Y value = 3.000, Our_Prediction = 3.600\n",
      "True Y value = 2.000, Our_Prediction = 2.800\n",
      "True Y value = 5.000, Our_Prediction = 4.400\n"
     ],
     "output_type": "stream"
    }
   ]
  },
  {
   "cell_type": "markdown",
   "source": [
    "epochs: 次数，我们这个模型不断通过学习training data,不断进行迭代更新coefficient\n",
    "\n",
    "\n",
    "$$ b = b - learning \\: rate*error *x$$\n",
    "\n",
    "1.把所有的epochs进行Loop<br>\n",
    "2.在每一个epoch里面训练集row进行循环 <br>\n",
    "3.每次循环进行coefficient调优\n",
    "\n",
    "$$b_0(t+1) = b_0(t)- learning \\: rate * error(t)$$<br>\n",
    "$$b_1(t+1) = b_1(t)- learning \\: rate * error(t)*x1(t)$$<br>\n",
    "$$error = prediction - expected $$"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}